import sys
from PySide6.QtGui import QFont
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QMainWindow
from ui.Agri_ui import Ui_MainWindow
from UIFuctions import UIFunctions
from pages.data_manager_ import DataManagerPage
from pages.analysis_tools_page import AnalysisToolsController  
from pages.config_page import ConfigPage
from pages.camera import CameraPage
from pages.camera_config import CameraConfigPage
from pages.user import User

class Window(Ui_MainWindow, QMainWindow):
    def __init__(self):
        super().__init__()
        
        # 初始化UI
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        # UI功能初始化
        self.ui_functions = UIFunctions(self)
        self.ui_functions.enable_dragging()
        
        # 初始化页面控制器
        # 在page_controllers字典中取消注释并添加
        self.page_controllers = {
            'home': None,  # 首页不需要特殊处理
            'Datamanager_2': DataManagerPage(self.ui.Datamanager_2),  # , 
            'Analysistools_2': AnalysisToolsController(self.ui.Analysistools_2),
            'config': ConfigPage(self.ui.config),
            'camera': CameraPage(self.ui.camera),
            'camera_config': CameraConfigPage(self.ui.camera_config),
            'user':User()
        }
        
        # 窗口设置
        self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        
        # 初始化信号连接
        self.init_slot()
        self.switch_page('home')  # 新增这行
        self.show()

    def init_slot(self):
        """初始化所有信号连接"""
        # 导航栏按钮
        self.ui.navigation.clicked.connect(
            lambda: UIFunctions.toggleMenu(self.ui_functions, self.ui, True))
        
        # 页面切换按钮
        page_buttons = {
            'Datamanager': 'Datamanager_2',
            'Analysistools': 'Analysistools_2',
            'ConfigProject': 'config',
            'camare': 'camera',
            'camare_config': 'camera_config',
            'home_2': 'home',
            'user': 'user'
        }
        
        for btn_name, page_name in page_buttons.items():
            button = getattr(self.ui, btn_name)
            if button:
                button.clicked.connect(lambda _, p=page_name: self.switch_page(p))

    def switch_page(self, page_name):
        """切换页面并激活对应控制器"""
        # 获取目标页面控件
        page_mapping = {
            'home': self.ui.home,
            'Datamanager_2': self.ui.Datamanager_2,
            'Analysistools_2': self.ui.Analysistools_2,
            'config': self.ui.config,
            'camera': self.ui.camera,
            'camera_config': self.ui.camera_config,
            'user': self.page_controllers['user']
        }
        
        target_page = page_mapping.get(page_name)
        if not target_page:
            return
        
        # 切换页面
        if isinstance(target_page, QMainWindow):  # 如果是 QMainWindow 类型
            target_page.show()
        else:
            self.ui.stackedWidget.setCurrentWidget(target_page)

        controller = self.page_controllers.get(page_name)
    
        if controller and hasattr(controller, 'on_page_show'):
            self.ui.stackedWidget.currentWidget().repaint()
            controller.on_page_show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 设置全局字体
    font = QFont("Microsoft YaHei", 10)
    app.setFont(font)
    window = Window()
    window.show()
    sys.exit(app.exec())